//
//  YijiaDetailViewController.m
//  HomeIn
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "YijiaDetailViewController.h"
#import "YijiaModel.h"
#import "YijiaDetailModel.h"
#import "TujiViewController.h"

@interface YijiaDetailViewController ()<UIWebViewDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, copy) NSString *title1;
@property (nonatomic, copy) NSString *ptime;
@property (nonatomic, copy) NSString *source;
@property (nonatomic, strong) UIView *aView;
@property (nonatomic, strong) UIScrollView *lunboScrollView;
@property (nonatomic, strong) UILabel *tujiLabel;

@end

@implementation YijiaDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor=[UIColor whiteColor];
    self.modelArray = [NSMutableArray array];
    
    //导航栏标题
    self.navigationItem.title = self.model.title;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //导航栏左侧按钮
    UIImage *image = [UIImage imageNamed:@"iconfont-unie642.png"];
    //取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction)];
    
    //网页
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    self.webView.scrollView.contentInset = UIEdgeInsetsMake(320*WIDTHSCALE, 0, 0, 0);
    
    self.aView = [[UIView alloc]initWithFrame:CGRectMake(self.webView.scrollView.frame.origin.x, self.webView.scrollView.frame.origin.y - 320*HEIGHTSCALE, 375*WIDTHSCALE, 320*HEIGHTSCALE)];
    [self.webView.scrollView addSubview:self.aView];
    
    self.lunboScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(20*WIDTHSCALE, 80*HEIGHTSCALE, 335*WIDTHSCALE, 235*HEIGHTSCALE)];
    [self.aView addSubview:self.lunboScrollView];
    self.lunboScrollView.showsHorizontalScrollIndicator = NO;
    self.lunboScrollView.showsVerticalScrollIndicator = NO;
    self.lunboScrollView.pagingEnabled = YES;
    self.lunboScrollView.delegate = self;
    
//    //请求数据
//    NSString *string = self.model.url_3w;
//    NSURL *url = [NSURL URLWithString:string];
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
//    [self.webView loadRequest:request];
   
    
    //解决webview到边框下拉有灰块
    for (UIScrollView* view in  self.webView.subviews)
    {
        if ([view isKindOfClass:[UIScrollView class]])
        {
            view.bounces = NO;
        }
    }
    self.webView.backgroundColor = [UIColor whiteColor];
    [self.webView setOpaque:NO];//设置控件是否透明
    
    
    
    [self getAFNetWorkingData:self.model.docid];
   
}

- (void)getAFNetWorkingData:(NSString *)ID
{
    
    NSString *urlString = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html", ID];
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSMutableDictionary *Dic = responseObject;
        NSMutableDictionary *Dic1 = [Dic objectForKey:self.model.docid];
        self.ptime = [Dic1 objectForKey:@"ptime"];
        self.source = [Dic1 objectForKey:@"source"];
        self.title1 = [Dic1 objectForKey:@"title"];
        [self.webView loadHTMLString:[Dic1 objectForKey:@"body"] baseURL:nil];
        
        NSMutableArray *array=[Dic1 objectForKey:@"img"];
        for (NSMutableDictionary *dic in array) {
            YijiaDetailModel *model = [[YijiaDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.modelArray addObject:model];
        }
        
        [self setAview];//方法传值
      
    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             NSLog(@"error = %@",error);
             
         }];
    
}

-(void)setAview
{
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10*WIDTHSCALE, 10*HEIGHTSCALE, 365*WIDTHSCALE, 50*HEIGHTSCALE)];
    titleLabel.text = self.title1;
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.numberOfLines = 2;
    [self.aView addSubview:titleLabel];
    
    self.tujiLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.aView.frame.origin.x + 20*WIDTHSCALE, 280*HEIGHTSCALE, 50*WIDTHSCALE, 30*HEIGHTSCALE)];
    [self.aView addSubview:self.tujiLabel];
    self.tujiLabel.text = @"图集";
    self.tujiLabel.backgroundColor = [UIColor blackColor];
    self.tujiLabel.textColor = [UIColor whiteColor];
    self.tujiLabel.font = [UIFont boldSystemFontOfSize:20];
    
    UILabel *ptimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(10*WIDTHSCALE, 60*HEIGHTSCALE, 100*WIDTHSCALE, 20*HEIGHTSCALE)];
    ptimeLabel.text = [self.ptime substringWithRange:NSMakeRange(5, 11)];
    ptimeLabel.font = [UIFont systemFontOfSize:14];
    [self.aView addSubview:ptimeLabel];
    
    UILabel *sourceLabel = [[UILabel alloc]initWithFrame:CGRectMake(110*WIDTHSCALE, 60*HEIGHTSCALE, 150*WIDTHSCALE, 20*HEIGHTSCALE)];
    sourceLabel.text = self.source;
    sourceLabel.font = [UIFont systemFontOfSize:14];
    [self.aView addSubview:sourceLabel];
    
    self.lunboScrollView.contentSize = CGSizeMake(335*WIDTHSCALE * self.modelArray.count, 0);
    for (int i = 0;i < self.modelArray.count;i++) {
        UIImageView *wangYiimageView = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTHSCALE*335*i, 0, 335*WIDTHSCALE, self.lunboScrollView.frame.size.height)];
        wangYiimageView.tag = i;
        [self.lunboScrollView addSubview:wangYiimageView];
        wangYiimageView.userInteractionEnabled = YES;
        
        YijiaDetailModel *model = [self.modelArray objectAtIndex:i];
        [wangYiimageView sd_setImageWithURL:[NSURL URLWithString:model.src] placeholderImage:nil];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [wangYiimageView addGestureRecognizer:tap];
        
        if (model.photosetID != nil) {
            self.tujiLabel.hidden = NO;
        }else
        {
            self.tujiLabel.hidden = YES;
        }
    }
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    UIImageView *imageView = (UIImageView *)tap.view;
    YijiaDetailModel *model = [self.modelArray objectAtIndex:imageView.tag];
    TujiViewController *tujiVC = [[TujiViewController alloc]init];
    if (model.photosetID != nil) {
        self.tujiLabel.hidden = NO;
        tujiVC.photosetID = [NSString stringWithFormat:@"    %@",model.photosetID];
        [self.navigationController pushViewController:tujiVC animated:YES];
    }else
    {
        self.tujiLabel.hidden=YES;
    }
    
}

- (void)leftBarAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

////webView字体大小
//- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    //     [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
//    [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'"];
//    
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
